pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
--sploosh demo
--by hp7hao

function _init()
	level=0
	levels=2
	respawn()
end

function _update()
	if state=="play" then
		update_plr()
	 update_pickups()
	 update_doors()
	 update_hazards()
	 update_enemies()
	elseif state=="dead" then
		if btnp(❎) then
			respawn()
			state="play"
		end
	elseif state=="start" then
		if btnp(❎) then
			state="play"
		end
	elseif state=="win" then
		if btnp(❎) then
			if level<levels then
				level+=1
			else
				level=0
			end
			respawn()
			state="play"
		end
	end
end

function _draw()
	cls()
	camera(128*level)
	map()
	draw_enemies()
	draw_plr()
	camera(0,0)
	draw_pickups()
	if state=="dead" then
		rectfill(38,58,88,68,1)
		print("try again!",44,61,10)
	elseif state=="start" then
		rectfill(38,58,88,68,1)
		print("press ❎",48,61,10)
	elseif state=="win" then
		rectfill(38,58,88,68,1)
		print("you win!",48,61,10)
	end
end
-->8
--player
function init_plr()
--setup player
	plr={
		x=20+128*level,
		y=20,
		dx=0,	--direction x
		f=false,	--flipped
		state="idle",
		sp=1	--sprite
	}
	
	jcd=0				--jump cooldown
	jforce=0	--jump force
	
	gravity=2
end

function update_plr()
--save player x location
	local lx=plr.x

--how the player responds to ctrls
	if btn(➡️) then
		plr.dx=1
		plr.f=false
		plr.state="crawl"
	elseif btn(⬅️) then
		plr.dx=-1
		plr.f=true
		plr.state="crawl"
	else
		plr.dx=0
		plr.state="idle"
	end
	
	if btnp(❎) then
		if(onground()) then
			jcd=5
			sfx(0)
		end
	end
	
	if btn(❎) then
		if jcd>0 then
			jforce=7
			jcd-=1
		end
	end
	
	plr.x+=plr.dx
	
	--test x collision
	if collidex() then
		plr.x=lx
	end
	
	--gravity
	if not onground() then
		plr.y+=gravity
		plr.state="jump"
	end
	--fix intersecting w/floor
	for i=1,gravity do
		if inground() then
			plr.y-=1
		end
	end
	
	--apply jump force
	if jforce>0 then
		jforce-=1
	end
	plr.y-=jforce
	
	if colceil() then
		jforce=0
		jcd=0
	end
	
	animate_plr()
	
end

function draw_plr()
--draw player
	spr(plr.sp,plr.x,plr.y,1,1,plr.f)
end

function onground()
	local txl=(plr.x+2)/8
	local txr=(plr.x+4)/8
	local ty=(plr.y+8)/8
	
	if mget(txl,ty)==11 or mget(txr,ty)==11 then
		return true
	else
		return false
	end
end

function collidex()
--collides with walls
	local txl=(plr.x+2)/8
	local txr=(plr.x+5)/8
	local ty=(plr.y+5)/8
	
	if mget(txl,ty)==11 or mget(txr,ty)==11 then
		return true
	else
		return false
	end
end

function colceil()
	local txl=(plr.x+2)/8
	local txr=(plr.x+6)/8
	local ty=(plr.y+2)/8
	
	if mget(txl,ty)==11 or mget(txr,ty)==11 then
		return true
	else
		return false
	end
end

function inground()
	local txl=(plr.x+4)/8
	local txr=(plr.x+4)/8
	local ty=(plr.y+7)/8
	
	if mget(txl,ty)==11 or mget(txr,ty)==11 then
		return true
	else
		return false
	end
end
-->8
--animation--

function animate_plr()
	if plr.state=="idle" then
		plr.sp=1
	elseif plr.state=="crawl" then
		if plr.sp<4 then
			plr.sp+=.2
		else
			plr.sp=1
		end
	elseif plr.state=="jump" then
		plr.sp=17
	elseif plr.state=="dead" then
		plr.sp=18
	end
end
-->8
--interactable--

function init_pickups()
	for x=0,47 do
		for y=0,47 do
			if mget(x,y)==14 then
				add(putbacks,{
					tx=x,
					ty=y,
					sp=14
				})
			end
		end
	end
end

function update_pickups()
	local tx=(plr.x+4)/8
	local ty=(plr.y+5)/8
	if mget(tx,ty)==14 then
		mset(tx,ty,9)
		point-=1
		sfx(1)
	end
end

function draw_pickups()
	print("point: "..point)
end

function update_doors()
	local tx=(plr.x+4)/8
	local ty=(plr.y+5)/8
	if mget(tx,ty)==33 then
		if point==0 then
			state="win"
		end
	end
end
-->8
--danger--

function init_enemies()
	
	enemies={}
	for x=0,47 do
			for y=0,47 do
				if mget(x,y)==25 then
					mset(x,y,9)
					add(enemies,{
						ex=x*8,
						ey=y*8,
						sp=25,
						ox=x*8,--origin on x
						dx=1 --direction on x
					})
					add(putbacks,{
						tx=x,
						ty=y,
						sp=25
					})
				end
			end
		end
end

function update_enemies()
	for e in all(enemies) do
		if abs(e.ex-e.ox)>16 then
			e.dx=e.dx*-1
		end
		e.ex+=e.dx*0.5
		
		-- move back and forth
		if anitimer<=0 then
			if e.sp==26 then
				e.sp=25
			elseif e.sp==25 then
				e.sp=26
			end
		end
		
		-- test if touch plr
		if abs(plr.x-e.ex)<8 and abs(plr.y-e.ey)<8 then
			plr.sp=18
			state="dead"
			sfx(2)
		end		
	end
end

function draw_enemies()
	for e in all(enemies) do
		if e.dx>0 then
			spr(e.sp,e.ex,e.ey,1,1,true,false)
		else
			spr(e.sp,e.ex,e.ey)
		end
	end
end

function update_hazards()
	local tx=(plr.x+4)/8
	local ty=(plr.y+5)/8
	if mget(tx,ty)==29 or mget(tx,ty)==30 then
		plr.sp=18
		state="dead"
		sfx(2)
	end
	
	--animate tiles
	if anitimer<=0 then
		--animate lava
		for x=0,47 do
			for y=0,47 do
				if mget(x,y)==29 then
					mset(x,y,30)
				elseif mget(x,y)==30 then
					mset(x,y,29)
				end
			end
		end
		anitimer=10
	else
		anitimer-=1
	end
end
-->8
--respown--

function respawn()
	for t in all(putbacks) do
		mset(t.tx,t.ty,t.sp)
		del(putbacks,t)
	end
	
	state="start"
	putbacks={}
	init_plr()
	init_enemies()
	init_pickups()
	anitimer=10
	point=2
end
__gfx__
0000000000000000000000000000000000000000000000000000000000000000cccccccccccccccc0bbbbbb0cffffffc0000000000000000cc1cc1cc00000000
0000000000000000000000000000000000000000000000000000000000000000ccccccccccccccccb3bbb33b2444444f003bbb0000000000c1311b1c00000000
0070070000001110000111000011100000011100000000000000000000000000ccc777cccccccccc443334442445454f003333b000000000cc13b1cc00000000
0007700000017bb10017bb10017bb1000017bb10000000000000000000000000cc77777ccccccccc444444542444444f003343b00bb000b0c1aaaa1c00000000
000770000017bba1017bba1017bba100017bba1000000000000000000000000077777777cccccccc454444442454545f003343b0003b0b301aaa9aa100000000
00700700001bbbb101bbbb101bbbb10001bbbb10000000000000000000000000c7777ccccccccccc444444442545454f000040000003b3001a9aaaa100000000
0000000001bbb1101bb331001bb331001bb33100000000000000000000000000cccccccccccccccc444454442454545f0002400000003000c1aaaa1c00000000
000000001bb110001b3310001b3310001b331000000000000000000000000000cccccccccccccccc04444440c222222c0002400000033000cc1111cc00000000
00000000000011100000000000000000000000000000000000000000000000000000000001111000000000000000000000000000999cccccccccc99900000000
0000000000017bb10000000000000000000000000000000000000000000000000000000019999100011110000000000000000000999999999999999900000000
000000000017bba10000000000000000000000000000000000000000000000000000000018989910199991000000000000000000998998999989989900000000
00000000001bbbb10000000000000000000000000000000000000000000000000000000019999910189899100000000000000000999989988998999900000000
00000000001bb3100000111000000000000000000000000000000000000000000000000017079991199999100000000000000000999999999999999900000000
0000000001bb310000115bb100000000000000000000000000000000000000000000000000e09991170799910000000000000000989898988989898900000000
0000000001b310000115bb5100000000000000000000000000000000000000000000000017079910170799910000000000000000898989899898989800000000
000000001b3100001333bbb100000000000000000000000000000000000000000000000019999910199999100000000000000000888888888888888800000000
00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c111111c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155551c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155551c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155551c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155151c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155551c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000c155551c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
090909090909090809090909090909090b09090909090909090909090909090b0b0909090909090b090909090909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090908090909090909090909090908090b09090909090909090909090909090b0b0909090909090b090909090809090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909090908090909090909090b09090909090909090909090909090b0b0909090809090b090909090b09090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909080909090909090909090b09090909090909090909090921090b0b0909090909090b09090b090909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909090909090909090909090b090909090909090909090b0b0b090b0b0909090909090b090909090909210b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090b0b0b0909090909090909091909090b09090909090909090909090909090b0b0909090909090b0b09090909090b0b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0909080b0b0b0909080e090b0b0b0b090b090909090909090e0919090909090b0b0909090909090b090909080909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0909090909090909090b0909090909090b09090909090b0b0b0b0b0b0909090b0b0909090909090b090909190909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909090909090909090909090b09090909090909090909090909090b0b0b0b0b0909090b090b0b0b0909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
09090909080909090909090909090e090b090b0b0b090909090909090909090b0b0909090e09190909090909090b0b0b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909090b0b0b0b0b090909090b09090909090909090909090909090b0b0909090b1d0b0b090909090908090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
090909090909090909090909090909090b09090909090e09190909090909090b0b090b0b0b0b0b09090909190909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0b0b09090909090909090909090921090b09090909090b0b0b0909090909090b0b0909090909090909090b0b0b0b090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0b0b0b0b1d1d1d1d1d1d1d1d0b0b0b0b0b0909090909090909090909090b090b0b09090909090909090909090909090b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b1d1d1d1d1d1d1d1d1d1d1d1d1d1d0b0b0b1d0b1d0b1d0b1d0b1d0b1d0b0e0b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
00010000070500705007050090500b0500d050100501305016050190501c05020050340001640013400114000f4000c4000940006400044000240000400004000d3000b3000a3000a30020000200002000000000
000100001d2501d2501d2501d2501d25034250342503c2503c2503c2503c250195001950019500195001950019500195000000000000000000000000000000000000000000000000000000000000000000000000
0010000026250242502325021250122500f2500d2500b250032000020000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
